

import logging
import os
import sys

import wsgiref.handlers

from google.appengine.api import mail

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp

class IndexPage(webapp.RequestHandler):
  def get(self):
    params = {'page': 'home'}
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, params))


class VisionPage(webapp.RequestHandler):
  def get(self):
    params = {'page': 'vision'}
    path = os.path.join(os.path.dirname(__file__), 'vision.html')
    self.response.out.write(template.render(path, params))


class AffiliatePage(webapp.RequestHandler):
  def get(self):
    params = {'page': 'affiliate'}
    path = os.path.join(os.path.dirname(__file__), 'affiliate.html')
    self.response.out.write(template.render(path, params))


class ProductsPage(webapp.RequestHandler):
  def get(self):
    params = {
      'page': 'products',
      'discount': self.request.get('discount', '')
    }
    path = os.path.join(os.path.dirname(__file__), 'products.html')
    self.response.out.write(template.render(path, params))

MAIL_FROM = 'derrickjsky@gmail.com'
MAIL_TO = 'derrickjsky+website@gmail.com'
MAIL_CC = 'lsimon+derricksky@commoner.com'
class ContactPage(webapp.RequestHandler):
  def post(self):
    sender = self.request.get('sender')
    subject = self.request.get('subject')
    body = '%s\nFrom: %s' % (self.request.get('body'), sender)
    success = False
    message = ''

    if sender == '' or subject == '' or body == '':
      message = 'You left one of the fields blank, try that again ...'
      params = {
        'page': 'contact',
        'message': message,
        'success': success,
        'sender': sender,
        'subject': subject,
        'body': body
      }
      path = os.path.join(os.path.dirname(__file__), 'contact.html')
      self.response.out.write(template.render(path, params))
      return

    if not mail.is_email_valid(sender):
      message = '%s is not a valid email address, try again please.' % sender
      params = {
        'page': 'contact',
        'message': message,
        'success': success,
        'sender': sender,
        'subject': subject,
        'body': body
      }
      path = os.path.join(os.path.dirname(__file__), 'contact.html')
      self.response.out.write(template.render(path, params))
      return

    try:
      mail.send_mail(sender=MAIL_FROM,
                     to=MAIL_TO,
                     cc=MAIL_CC,
                     subject=subject,
                     body=body)
      success = True
    except:
      e = sys.exc_info()[1]
      logging.error('Error sending mail %s' % e)
      message = 'Drats, we were not able to send your email. %s' % e

    params = {
      'page': 'contact',
      'message': message,
      'success': success,
      'sender': sender,
      'subject': subject,
      'body': body
    }
    path = os.path.join(os.path.dirname(__file__), 'contact.html')
    self.response.out.write(template.render(path, params))

  def get(self):
    params = {'page': 'contact', 'success': False}
    path = os.path.join(os.path.dirname(__file__), 'contact.html')
    self.response.out.write(template.render(path, params))


def main():
  application = webapp.WSGIApplication(
                                       [('/', IndexPage),
                                        ('/vision', VisionPage),
                                        ('/products', ProductsPage),
                                        ('/contact', ContactPage),
                                        ('/affiliate', AffiliatePage),
                                        ('/.*', ProductsPage)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
